using System;
using System.Net;
using Game500.ClientServerCommon.Src;
using log4net;

namespace MtServer.Src
{
  internal static class MtServer
  {
    private static readonly ILog Log = LogManager.GetLogger(typeof (MtServer));

    private static void Main(string[] args)
    {
      string serverName;
      int port;
      IPAddress address;
      try
      {
        var bag = new ArgumentBag(args);
        serverName = bag.GetValue("-n", "NoName");
        port = int.Parse(bag.GetValue("-p", "5050"));
        address = IPAddress.Parse(bag.GetValue("-a", "127.0.0.1"));
      }
      catch (Exception e)
      {
        Log.Fatal("Failed to parse command line: " + e.Message);
        PrintUsage();
        return;
      }


      Log.InfoFormat("Starting multi-threaded server: '{0}'", serverName);
      var mm = new BaseMapManager();
      var frontend = new ServerFrontend(port, serverName, address, mm);
      frontend.Start();
    }

    private static void PrintUsage()
    {
      Console.WriteLine("USAGE:\n  MtServer [-n servername] [-p port] [-a address]\n");
      Console.WriteLine("  -n\t Server name. Will be displayed to client. Default is 'NoName'");
      Console.WriteLine("  -p\t Local port. Default is 5050");
      Console.WriteLine("  -a\t Remote address. Default is 127.0.0.1");
    }
  }
}